<?php
/**
 * Created by JetBrains PhpStorm.
 * User: developer
 * Date: 10/15/12
 * Time: 5:04 PM
 * To change this template use File | Settings | File Templates.
 */
class Page_Be_Products_ProductsPoems extends Page {

	/**
	 * @var Db_Products_ProductsPoems
	 */
	protected $tableObject;

	public function __construct() {
		$this->actionsTableObject = array(
			'ajaxSaveAction'
		);
		$this->classTableObject = 'Db_Products_ProductsPoems';
		parent::__construct();
	}

	public function defaultAction() {
		perm('PRODUCTS_POEMS_VIEW');
		self::$TPL->appendJsFile('admin/products_poems');
		$poems = Db_Products_Poems::findAllArray();
		self::$TPL->assign('poems', $poems);
		self::$TPL->setBodyTemplate('list_products_poems');

		return array(
			'type' => 'display'
		);
	}

	public function ajaxLoadProductsPoemsDataAction() {

		$allColumns = array('p_name');
		$searchColumns = array('p_name');
		list($searchQuery,$orderBy, $limit) = Utils::getSqlQueriesForPagination($allColumns, $searchColumns, 'Db_Products_Products', 'getTotalProducts');
		$allProducts = Db_Products_Products::getAllProducts($limit, $searchQuery, $orderBy);
		$allProductsPoems = Db_Products_ProductsPoems::getCheckedProductsPoems();
		$allPoemIds = Db_Products_Poems::getAllPoemIds();

		self::$TPL->assign('allProducts', $allProducts);
		self::$TPL->assign('allPoemIds', $allPoemIds);
		self::$TPL->assign('allProductsPoems', $allProductsPoems);
		return array(
			'type' => 'encoded_json',
			'content' => self::$TPL->display('json_products_poems', true)
		);
	}

	public function ajaxSaveAction() {
		$productId = post('pps_product_id', 'int');
		$poemId = post('pps_poem_id');
		$delete = post('delete');
		if(!isset($delete) && perm('PRODUCTS_POEMS_EDIT')) {
			$this->tableObject->pps_product_id = $productId;
			$this->tableObject->pps_poem_id = $poemId;
			if(!$this->tableObject->save()) {
				$this->error = true;
			}
		} else if(perm('PRODUCTS_POEMS_EDIT')) {

				if(!Db_Products_ProductsPoems::deleteProductsPoems($productId, $poemId)) {
					$this->error = true;
				}
		} else {
			$this->error = true;
		}
		return array(
			'type' => 'encoded_json',
			'content' => $this->jsonResult($this->error, true, !$this->error)
		);
	}


}























